/*
 * Copyright (c) 2016. zhaojin 15398699939@163.com
 */

package com.bankscene.screenfit.vus;

import android.graphics.drawable.Drawable;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.bankscene.screenfit.R;
import com.bankscene.screenfit.events.EventBusEvents;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindColor;
import butterknife.BindDrawable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnItemClick;

/**
 * Created by jiiiiiin on 6/14/16.
 */
public class MenusListViewVu extends BaseVu implements Vu {

    @BindView(R.id.listview)
    ListView listview;
    @BindDrawable(R.drawable.menuslistviewitemselected_shape)
    Drawable selectedMenuStyle;
    @BindDrawable(R.drawable.menuslistviewitemunselected_shape)
    Drawable unselectedMenuStyle;
    @BindColor(R.color.primary_text)
    int selectedMenuFontColor;
    @BindColor(R.color.secondary_text)
    int unselectedMenuFontColor;

    private View selectedView;//缓存被选中的item

    @Override
    public void init(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable EventBus bus) {
        this.bus = bus;
        rootView = inflater.inflate(R.layout.menuslistview_fragment, container, false);
        unbinder = ButterKnife.bind(this, rootView);
    }

    @OnItemClick(R.id.listview)
    void OnItemClick(View view, int position) {
        selectedView = view;
        bus.post(new EventBusEvents.OnItemClickEvent(position));
    }

    public void setListAdapter(@NonNull ListAdapter adapter) {
        listview.setAdapter(adapter);
    }

    @CheckResult
    public View getSelectedView() {
        return selectedView;
    }

    public void clearOldItemStyleOnItemSelected(@NonNull View convertView) {
        convertView.setBackground(unselectedMenuStyle);
        MenusListViewItemVu menusListViewItemVu = (MenusListViewItemVu) convertView.getTag();
        menusListViewItemVu.getTitleView().setTextColor(unselectedMenuFontColor);
    }

    public void updateItemStyleOnItemSelected(@NonNull View convertView) {
        convertView.setSelected(true);
        convertView.setBackground(selectedMenuStyle);
//        convertView.setBackgroundDrawable(selectedMenuStyle);//本应用要支持api11故使用改接口
        //找到选中的holderview
        MenusListViewItemVu menusListViewItemVu = (MenusListViewItemVu) convertView.getTag();
        menusListViewItemVu.getTitleView().setTextColor(selectedMenuFontColor);
    }
}
